package ext.play;

import org.apache.commons.lang.StringUtils;
import play.i18n.Messages;
import play.templates.BaseTemplate;
import play.templates.JavaExtensions;
import util.common.DateUtil;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 页面上Html文字相关处理的扩展.
 */
public class HtmlExtension extends JavaExtensions {

    public static Boolean isEmpty(String value) {
        return StringUtils.isEmpty(value);
    }

    public static Boolean isNotEmpty(String value) {
        return StringUtils.isNotEmpty(value);
    }

    public static Boolean isBlank(String value) {
        return StringUtils.isBlank(value);
    }

    public static Boolean isNotBlank(String value) {
        return StringUtils.isNotBlank(value);
    }

    /**
     * 把回车换行换成HTML段落.
     */
    public static BaseTemplate.RawData nrToParagraph(String value) {
        String[] splits = value.split("\\r?\\n|\\r");
        return new BaseTemplate.RawData(Arrays.stream(splits).map(v -> "<p>" + v + "</p>").collect(Collectors.joining("")));
    }

    /**
     * 把回车换行换成<br/>，支持Mac/Win/Linux格式风格.
     */
    public static BaseTemplate.RawData nrToBr2(String value) {
        String[] splits = value.split("\\r?\\n|\\r");
        return new BaseTemplate.RawData(Arrays.stream(splits).collect(Collectors.joining("<br/>")));
    }

    public static Boolean contains(Integer[] values, Integer value) {
        for (Integer iv : values) {
            if (iv != null && iv.equals(value)) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    public static String truncate(String value, Integer length) {
        if (StringUtils.isBlank(value)) {
            return "";
        }

        if (value.length() >= length) {
            return value.substring(0, length) + "...";
        }

        return value;
    }

    public static String round(BigDecimal amount, Integer scale) {
        return String.valueOf(amount.setScale(scale, BigDecimal.ROUND_HALF_DOWN));
    }


    public static String round(BigDecimal amount) {
        return String.valueOf(amount.setScale(0, BigDecimal.ROUND_HALF_DOWN));
    }


    /**
     * 是否今天?
     */
    public static boolean isToday(Date date) {
        return DateUtil.compareDate(date, DateUtil.getEndOfDay(new Date()));
    }

    /**
     * 格式化LocalDate.
     */
    public static String format(LocalDate date, String pattern) {
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * LocalDate是周末
     */
    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek == DayOfWeek.SUNDAY || dayOfWeek == DayOfWeek.SATURDAY;
    }

    public static String i18nYesNo(Object object) {
        return JavaExtensions.yesno(object, new String[]{Messages.get("yesno.yes"), Messages.get("yesno.no")});
    }

    /**
     * 截断BigDecimal后面的0.
     */
    public static String stripZeros(BigDecimal bigDecimal) {
        if (bigDecimal == null) {
            return "";
        }
        return bigDecimal.setScale(4, BigDecimal.ROUND_HALF_DOWN).stripTrailingZeros().toPlainString();
    }
}
